<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller {
	public function __construct() {
		parent::__construct();
	} 
	public function index() {
        $this->load->library("Douban_api");
        $q = filter_input(INPUT_GET,'q',FILTER_SANITIZE_SPECIAL_CHARS);
        $page = filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
        $count = filter_input(INPUT_GET,'n',FILTER_VALIDATE_INT);
       // dump($_SERVER);
        $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PATH_INFO"];
        if(!empty($q)){
            $url .= "?q=".urlencode($q); 
        }
        $data["url"] = $url;
        $page = $page>0?$page:1;
        $count = $count>0?$count:20;
        $start = ($page-1)*$count;
        $q = urldecode($q);
        $data["q"] = iconv('','gbk//IGNORE',$q);
        
        $douban_api = Douban_api::getInstance();
        $book_data = $douban_api->getSearhResults($q,$start,$count);
        $data["total"] = ceil($book_data->total/$count);
        $data["page"] = $page;
        $data["book_data"] = isset($book_data->books)&&!empty($book_data->books)?$book_data->books:null; 
      //dump($book_data);     
		$this->load->view('search',$data);
	} 
} 
?>